package com.future.cglib.v2;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Description:
 *
 * @author weiruibai.vendor
 * Date: 2021/9/8 15:30
 */
public class MyCglibProxy implements MethodInterceptor {

    private Enhancer enhancer = new Enhancer();

    private String name;

    public MyCglibProxy(String name) {
        this.name = name;
    }

    public Object getDaoBean(Class clacz) {
        enhancer.setSuperclass(clacz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        if (!"张三".equals(name)) {
            System.out.println("没有权限");
            return null;
        }
        Object result = null;
        System.out.println(" 开始执行");
        result = proxy.invokeSuper(obj, args);
        System.out.println("执行完毕");
        return result;
    }
}
